home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Ham Radio 2000
/
Ham Radio 2000.iso
/
ham2000
/
packet
/
p_tapr
/
tnchst
/
mthcode.c
< prev
next >
Wrap
Text File
|
1991-08-18
|
13KB
|
514 lines
/** MTHOST - TNC-2 binary interface main module
(c) 1989-91 Howard Goldstein **/
extern void near cdecl COM_START(void);
extern void near cdecl REMOVE_VECTOR(void);
extern int near cdecl FILETOTNC(char *);
#define BIT8N FALSE
union REGS regblock, retblk ,*auxr = ®block ; /* for intdos call */
FILE *upfile, *dwnfile ; /* file pointers */
char fn[80] ; /* store filenames here */
char multi_name[20] ; /* filename for multi
downlaods here */
long free_for_download ; /* limit to amount free to dwnld */
int tnc, kbd ;
char *dmys ; /* dummy string ptr, for
lint in set_up/dwn */
int dmy ; /* dmy int var */
/** DLC ADDRESS **/
byte mydlcnum = 0 ; /* must be 0<num<254. TNCs should start at 1,
hosts at 254. Addressing off = 0 */
long int disk_free()
{
auxr->h.ah = 0x36 ; /* disk space */
auxr->h.dl = 0 ; /* get curr drive */
intdos( auxr , &retblk ); /* get space */
return ( (long) retblk.x.ax * (long) retblk.x.bx *
(long) retblk.x.cx ) ;
/* return the free space */
}
void updonemsg()
{
printf("\007\n*Upload complete*\n");
}
int donada( dmy )
int dmy;
{
signal( SIGINT, donada ) ;
PUTAUX(3); return(3); }
void print_help() /* print help info */
{
printf("\
\r\n\r\n TNC-2 binary intfc test terminal program (11 Jan 91 n2wx)\
\r\n\r\n");
printf("\r\n\
Key Function\r\n\
--- --------\r\n\
PGUP Upload a file to the TNC\r\n");
printf("\
PGDWN Download a file from the TNC\r\n\
(note upload and download are not as they seem and irrelevant re: BLP\r\n\
\r\n\
ESC Close any open upload and/or download files\r\n\
F1 Set DLC Address (0=dlc addressing off, 1-254 valid)\r\n");
printf("\
F2 Initiate Connect (cnct to TNCCMD to get cmd\r\n\
F3 Send data on a BLP channel\n\r\
F4 Disconnect\n\r\
F5 Send the canned UI frame (A>B:xxxx)\n\r\
F6 Send conn-stat-enq to selected channel\n\r\
\r\n\
Alt-X Exit the program, return to DOS\r\n\r\n\r\n");
}
void set_upfile()
{
static struct timeb lasttimeb, thistimeb ; /* keep track of secs */
static unsigned long filsiz ; /* track filesize too */
double lasttime,thistime;
double cps ; /* chars per sec computed */
double rola = 0.0; /* rolling avg accum */
FILE *ts; /* temporary file ptr for checking size */
if ( upfile ) /* already doing it? */
return ; /* yes, ignore upld cmd */
printf("\r\nFile to upload:");
dmys = gets(fn); /* get the string */
/* if ( strlen( fn ) > 0 ) /* if somethign here */
if ( ! ( ts = fopen( fn , "r" ) ) )
{
set_upfile(); return; /* recursively call and ret if err */
}
filsiz = filelength( fileno( ts ) ) ; /* get long file len */
fclose( ts) ;
ftime(&thistimeb); /* initialize so as to not bias
the moving average too much */
#if 1
do
{
memcpy((char *) &lasttimeb,(char *) &thistimeb,
sizeof(struct timeb) );
/* get secs fm last */
ftime(&thistimeb); /* get # secs now */
lasttime = ((double) lasttimeb.time)+ ((double)lasttimeb.millitm /1000.0);
thistime = ((double) thistimeb.time)+ ((double) thistimeb.millitm /1000.0);
/* compute cps as a double */
if ( lasttimeb.time==thistimeb.time )
{ rola = cps = 960.0 ; }
else
cps = (double) filsiz /
(thistime-lasttime) ;
/* compute rolling avg */
rola = (rola * .8) + ( cps * .2 ) ;
printf("%lu chars in %.2f seconds = %.1fcps, avg %.2f\n",
filsiz, thistime-lasttime, cps , rola );
/* updonemsg(); /* uplaod done if came back ok */
}
while ( ftime(&thistimeb), ! FILETOTNC(fn) && !kbhit() ) ;
#else
if ( ! ( upfile = fopen( fn , "rb" ) ) ) /* and if NOT able to open */
{
printf("%s not found!\r\n",fn) ;
set_upfile() ; /* show err and do again */
}
#endif
}
void set_dwnfile()
{
if ( dwnfile ) /* already doing it? */
return ; /* yes, ignore upld cmd */
free_for_download = disk_free() - MINFREE ; /* lv something */
printf("\r\nFile to download:");
dmys = gets( fn ) ; /* get the string */
if ( strlen( fn ) > 0 ) /* if somethign here */
if ( ! ( dwnfile = fopen( fn , "wb" ) ) ) /* and if NOT able to open */
{
printf("Can't open %s!\r\n",fn) ;
set_dwnfile() ; /* show err and do again */
}
}
void cdecl kbd_blp_call() /* issue outgoing call */
{
struct blpcb_struct *blpcb; /* get new blpcb from blp */
char addr[80];
printf("Enter address to call; precede with '!' to get email:");
dmys = gets( addr );
blpcb = blp_start_cmd( nextchan, addr ); /* try to setup */
if ( blpcb ) /* if was able to set up */
{
blp_intfc[nextchan] = blpcb ; /* save ptr to intfc */
nextchan++; /* bump to next chan */
printf("calling...");
}
else printf("*** kbd_blp_call(): couldn't start a blp\n");
}
void cdecl kbd_blp_data() /* send outgoing data */
{
char temp[256],data[258];
int send_stat; /* return stat from send attempt */
int chan; /* chan # to send to */
printf("chan # [space] Data (max 256):");
dmys = gets( temp );
if (strlen(temp) < 3 )
strcpy(temp,"0 ");
/* get chan # and store data */
sscanf( temp, "%d", &chan );
/* cr append if not cmd channel */
switch ( blp_intfc[chan]->lcn )
{ case 0x70: case 0x71: case 0x72:
break;
default:
strcat( temp , "\r" ); /* force cr on end */
}
/* send the data */
send_stat = blp_data_cmd( blp_intfc[chan], temp+2, strlen(temp+2) );
if ( !send_stat ) /* if err */
printf("Error sending data\n");
}
#if 0
byte stdframe[] = {
0x84,0x40,0x40,0x40,0x40,0x40,0x60,
0x84,0x40,0x40,0x40,0x40,0x40,0x60,
0x82,0x40,0x40,0x40,0x40,0x40,0x61,
0x03,0xf0,
"1234567890123456789012345678901234567890123456789012345678901234567890\
1234567890123456789012345678901234567890123456789012345678901234567890\
1234567890123456789012345678901234567890123456789012345678901234567890\
1234567890123456789001234567890test\r"};
#else
/* tiny test frame*/
byte stdframe[] = {
0x84,0x40,0x40,0x40,0x40,0x40,0x60,
0x84,0x40,0x40,0x40,0x40,0x40,0x60,
0x82,0x40,0x40,0x40,0x40,0x40,0x61,
0x03,0xf0,"\
012345678901234567890123456789012345678\r"};
#endif
void cdecl kbd_blp_datagram()
{
char temp[256],data[258];
int send_stat,chan; /* return stat from send attempt */
/* printf("lcn #:");
dmys = gets( temp );
if (strlen(temp) < 3 )
strcpy(temp,"0 ");
sscanf( temp, "%d", &chan );
*/
printf("sending test packet to channel 113d (71H)\n");
chan = 0x71;
/* send the data */
send_stat = send_blp_datagram( chan, stdframe, sizeof( stdframe ));
if ( !send_stat ) /* if err */
printf("Error sending data\n");
}
void cdecl kbd_blp_stop()
{
char temp[20];
int chan;
printf("chan # to stop:");
dmys=gets(temp);
sscanf(dmys,"%d",&chan);
blp_stop_cmd( blp_intfc[chan] );
}
void cdecl kbd_send_cstenq()
{
char temp[20];
int chan;
printf("chan # to enquire:");
dmys=gets(temp);
sscanf(dmys,"%d",&chan);
blp_status_cmd( blp_intfc[chan] );
}
void cdecl set_dlc_addr()
{
char temp[20];
printf("Address is now %d. New address: ",mydlcnum);
dmys = gets(temp);
sscanf(dmys,"%d",&mydlcnum);
}
void cdecl cmdproc( firstc )
int firstc; /* enter with 0 for extended or ESC */
{ /* for escape */
void cdecl paint_disk_file(); /* later in this module, invokes
paint fm file procedure */
void cdecl paint_printer(); /* same as above */
static char fn[ 80 ] ; /* store fn here, static so we don't
stack overflow too soon on recurs */
static int xkey ; /* if extended, nxt code put here */
if ( firstc == ESC ) /* if it was escape */
{
if ( dwnfile )
{
fputc( EOFCH , dwnfile );
fclose( dwnfile ); /* close up file, if open */
}
if ( upfile )
{
printf("\007\n*Upload aborted*\n");
fclose( upfile ); /* down file too */
}
dwnfile = upfile = (FILE *) NULL ;
/* zap pointers */
}
else if ( firstc == 0 ) /* was extended char ?*/
{
xkey = getch() ; /* get the extended key */
switch ( xkey ) /* based on extended key... */
{
case PGUP: /* upload */
set_upfile() ; break ;
case PGDWN:
set_dwnfile(); break ;
case F1:
set_dlc_addr();break; /* set address */
case F2:
kbd_blp_call(); break; /* call setup */
case F3:
kbd_blp_data(); break; /* send blp data */
case F4:
kbd_blp_stop();
break;
case F5: /* send datagram */
kbd_blp_datagram(); break;
case F6: /* send cstenq */
kbd_send_cstenq(); break;
case ALTX:
cmdproc( ESC ) ; /* close files */
REMOVE_VECTOR(); /* remove vec */
exit(0) ; /* bye */
default:
print_help(); break ;
}
}
}
void totnc_proc()
{
if ( kbhit() )
{
kbd = getch(); /* just get the char,
used to echo directly */
putchar( kbd ); /* echo if not paint */
switch ( kbd )
{
case 0:
case ESC:
cmdproc( kbd ) ;
break ;
/* if extended key or */
/* escape */
/* then proc as cmd */
case CR:
putchar('\n'); /* echo newline */
/* fall thru too */
default:
PUTAUX( kbd ) ; /* else send to tnc*/
}
}
while ( upfile && AUXRDY() ) /* upldng and rdy? */
{
kbd = fgetc( upfile ) ;
if ( kbd != EOFCH ) /* not EOF? */
{
PUTAUX( kbd );
}
else /* eof */
{
/* fclose( upfile ) ; */
/* updonemsg(); /* say upload complete*/
/* upfile = (FILE *) NULL ; */
/* keep going till abort */
}
}
}
void fmtnc_proc()
{
if ( AUXAVAIL() ) /* if fm tnc rdy */
{
tnc = ( GETAUX() ) ; /* get char */
putchar( tnc );
aftr_proc( tnc ); /* send to aft handler */
if ( dwnfile ) /* dwnldng? */
{
fputc( tnc , dwnfile ) ; /* send it if so */
if ( free_for_download-- < 0L ) /* if disk full */
{
fclose( dwnfile );
dwnfile = NULL ;
printf("\n\r\007DISK FULL!, minimum %d bytes needed. Download cancelled.\r\n",
MINFREE);
}
}
} /*endif*/
}
blp_tick_test()
{
static long lasttime;
long thistime ;
/** upfile holdoff below -- DONT put in normal host, use it here
so n2wx can continue to use this prog for timing... **/
if ( !upfile && time(&thistime) != lasttime ) /* if a second elapsed */
{
lasttime = thistime ; /* update time */
blp_timer_tick(); /* do the tick */
dlc_timer_tick();
}
}
main(argc,argv)
int argc;
char *argv[];
{
int x;
COM_START(); /* init the COM1
hardware/driver */
auxr->x.dx = COMPORT ; /* com1 */
auxr->h.ah = 0 ; /* init */
/* e3 IS 9600/N/8/1 C3 IS 4800/N/8/1 */
/* auxr->h.al = 0xC3 ; /* 4800,n,8,1 */
/* auxr->h.al = 0x33 ; /* 19.2,n,8,1 */
auxr->h.al = 0xe3 ; /* 9600,n,8,1 */
/* auxr->h.al = 0x9A ; /* 1200,e,7,1 */
/* auxr->h.al = 0xBA ; /* 2400 */
int86( 0x14 , auxr , &retblk ); /* set up com port */
printf("\nInitializing COM1 for %x (9a=1200,ba=2400e7,c3=4800n8,e3=9.6k,33=19.2)\n",
auxr->h.al);
while (!AUXRDY())
{
printf("\n\n** TNC doesn't seem ready! (hardware flow control showing\
busy - check cable/TNC power etc.\n\007");
for ( x = 0 ; x < 30000 && !AUXRDY() && !kbhit() ; x++ ) ;
}
signal( SIGINT, donada ); /* kill ^C int */
dwnfile = upfile = (FILE *) NULL ; /* zap all file ptrs */
aftr_init();
dlc_init();
dlc_start_cmd(); /** inits dlc**/
blp_init();
PUTAUX( 'Q'-0x40 ); /* unassert possible STOP left by PTP */
/* do forever terminal program */
while ( 1 )
{
totnc_proc();
fmtnc_proc();
blp_tick_test();
}
}